package com.hzz.web.boss;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hzz.api.analysis.IAnalysisEventComplaintService;
import com.hzz.api.analysis.IAnalysisRiverSupervisorService;
import com.hzz.api.examine.IExamineResultService;
import com.hzz.api.patrol.IPatrolTaskService;
import com.hzz.api.river.IChannelInfoService;
import com.hzz.api.river.IReservoirInfoService;
import com.hzz.api.river.IRiverInfoService;
import com.hzz.dto.analysis.AnalysisEventComplaintDto;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.NoAuthorize;
import com.hzz.framework.util.DateUtil;
import com.hzz.framework.util.OnLineCacheUtil;
import com.hzz.framework.web.controller.BaseController;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "首页")
@RestController
@Slf4j
public class IndexController extends BaseController {
    private static final String acPrefix = "/boss/";
    
    @Autowired
    IRiverInfoService riverInfoService;
    
    @Autowired
    IReservoirInfoService reservoirInfoService;
    
    @Autowired
    IAnalysisRiverSupervisorService analysisRiverSupervisorService;
    
    @Autowired
    IAnalysisEventComplaintService analysisEventComplaintService;
    
    @Autowired
    IPatrolTaskService patrolTaskService;

	@Autowired
	IExamineResultService examineResultService;

	@Autowired
	IChannelInfoService channelInfoService;
    
    @NoAuthorize
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST }, value = "/")
    public void index() throws Exception {
        log.info("index");
        // response.sendRedirect("/actuator/info");
        response.sendRedirect("/index.html");
    }
    
    @NoAuthorize
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST }, value = "/info")
    public void info() throws Exception {
        log.info("info");
        response.sendRedirect("/actuator/info");
    }
    
    /**
     * 综合统计
     */
	// @NoAuthorize
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "stats/all")
    public Response statsAll() {
        log.info("statsAll.........");
        Response result = new Response(0, "success");
        try {
            Map<String, Object> dataMap = new HashMap<>();
            // 河段总数
			dataMap.put("statsRiver", riverInfoService.getTotalNum(1));
			// 渠道总数
			dataMap.put("statsChannel", channelInfoService.getTotalNum());
            // 湖库总数
            dataMap.put("statsRreservoir", reservoirInfoService.getTotalNum());
            // 河长总数
            dataMap.put("statsRiverSupervisor", analysisRiverSupervisorService.analysisRiverSupervisor(null));
            // 在线河长
            dataMap.put("statsOnline", OnLineCacheUtil.size());
            // 河长巡查率 前5名
            dataMap.put("statsInspectionRate0", patrolTaskService.getInspectionRateDESC());
            // 河长巡查率 后5名
            dataMap.put("statsInspectionRate1", patrolTaskService.getInspectionRateASC());
            // 河长考核 前5名
			dataMap.put("statsExamine0", examineResultService.getLastYearRank5(1));
            // 河长考核 后5名
			dataMap.put("statsExamine1", examineResultService.getLastYearRank5(2));
			// 河长考核年度
			dataMap.put("examineYear", DateUtil.getLastYear());
            // 公共投诉
            AnalysisEventComplaintDto dto = new AnalysisEventComplaintDto();
            dto.setReportingType(1);
            dataMap.put("statsComplaint", analysisEventComplaintService.statsComplaint(dto));
            // --
            result.data = dataMap;
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }
}
